<?php

namespace App\Jobs\SubAccount;

use App\Models\SubAccount\SubAccountLoginLogoutRecord;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SubAccountForgetProcessJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //处理漏上号，漏登记
        $records=$this->getLoginRecords();
        if(count($records)){
            foreach ($records as $r){
                if($r->login_time==null){
                    $r->is_forget_login=1;
                    if($r->account){
                        $r->account->increment('forget_login_quantity');
                    }
                }
                if($r->is_data_recorded==0){
                    $r->is_forget_record=1;
                    if($r->account){
                        $r->account->increment('forget_record_quantity');
                    }
                }
                if($r->isDirty()){
                    $r->save();
                }
            }
        }

    }

    //获取要处理的记录
    public function getLoginRecords(){
        $yesterday=date('Y-m-d',strtotime('-1 day'));
        return SubAccountLoginLogoutRecord::where('log_in_out_date',$yesterday)->get();
    }

}
